
package Playlist_Management;

/**
 *
 * @author GeorgeD
 */

import java.util.List;
import javax.swing.ListSelectionModel;

public class PlayListSongSearch extends javax.swing.JFrame {
    private PlayListActions playlistSongTableModel;
    private PlayListManagementForm PlayListManagement_Form;    
    
    /**
     * Creates new form SongSearch
     */
    public PlayListSongSearch(PlayListManagementForm PlayListManagement_Form, PlayListActions playlistSongTableModel) {
        this.PlayListManagement_Form = PlayListManagement_Form;
        this.playlistSongTableModel = playlistSongTableModel;
        initComponents();
        setLocationRelativeTo(null);//για την εμφάνιση του παραθύρου στο κέντρο
        this.SongListTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        Radiostation_ProjectPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("Radiostation_ProjectPU").createEntityManager();
        query1 = Radiostation_ProjectPUEntityManager.createQuery("SELECT NEW Playlist_Management.PlayListSongMeta(sng, art, mcg) FROM Song sng JOIN sng.albumId alb LEFT JOIN alb.musicgroup mcg LEFT JOIN alb.artist art ");
        list1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query1.getResultList());
        jScrollPane1 = new javax.swing.JScrollPane();
        SongListTable = new javax.swing.JTable();
        SaveBtn = new javax.swing.JButton();
        CancelBtn = new javax.swing.JButton();
        SaveBtn1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        lbl1 = new javax.swing.JLabel();
        ErrorMessage = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Πίνακας Τραγουδιών");

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list1, SongListTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${song.songId}"));
        columnBinding.setColumnName("ID");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${song.title}"));
        columnBinding.setColumnName("ΤΙΤΛΟΣ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${artist.artisticname}"));
        columnBinding.setColumnName("ΚΑΛΛΙΤΕΧΝΗΣ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${musicgroup.name}"));
        columnBinding.setColumnName("ΣΥΓΚΡΟΤΗΜΑ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${song.duration}"));
        columnBinding.setColumnName("ΔΙΑΡΚΕΙΑ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(SongListTable);

        SaveBtn.setText("Καταχώρηση");
        SaveBtn.setToolTipText("");
        SaveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddBtnActionPerformed(evt);
            }
        });

        CancelBtn.setText("Ακύρωση");
        CancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelBtnActionPerformed(evt);
            }
        });

        SaveBtn1.setText("Αναζήτηση");
        SaveBtn1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchBtnActionPerformed(evt);
            }
        });

        jLabel1.setText("(Αναζήτηση με βάση τον Τίτλο, τον Καλλιτέχνη ή το Όνομα Συγκροτήματος)");
        jLabel1.setToolTipText("");

        lbl1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lbl1.setForeground(new java.awt.Color(0, 0, 0));
        lbl1.setText("Κλειδί Αναζήτησης:");
        lbl1.setToolTipText("");

        ErrorMessage.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        ErrorMessage.setForeground(new java.awt.Color(255, 0, 0));
        ErrorMessage.setToolTipText("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 721, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbl1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(SaveBtn1)
                        .addGap(37, 37, 37)
                        .addComponent(SaveBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(CancelBtn))
                    .addComponent(ErrorMessage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 619, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(265, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SaveBtn)
                    .addComponent(CancelBtn)
                    .addComponent(SaveBtn1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbl1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ErrorMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(87, Short.MAX_VALUE)))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void AddBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddBtnActionPerformed
        // TODO add your handling code here:
        ErrorMessage.setText("");
        int index = SongListTable.getSelectedRow();
        if(index >= 0){
           Integer songId = (Integer)this.SongListTable.getModel().getValueAt(index, 0);
        for(PlayListSongMeta PlayList_SongMeta:list1){
            if( PlayList_SongMeta.getSong().getSongId() == songId ){
                this.PlayListManagement_Form.getModelPlayListManageTable().addRow(PlayList_SongMeta);
                this.PlayListManagement_Form.setEnabled(true);
            }
        }
        dispose(); 
        } else {            
            ErrorMessage.setText("Παρακαλώ επιλέξτε ένα Τραγούδι από τον παραπάνω πίνακα");
        }        
    }//GEN-LAST:event_AddBtnActionPerformed

    private void CancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelBtnActionPerformed
        // TODO add your handling code here:
        this.PlayListManagement_Form.setEnabled(true);
        dispose();
    }//GEN-LAST:event_CancelBtnActionPerformed

    private void SearchBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SearchBtnActionPerformed
        // TODO add your handling code here:
        String key_str = this.jTextField1.getText().trim();
        List<PlayListSongMeta> PlayList_SongMeta = this.PlayListManagement_Form.GetRadiostation_ProjectPUEntityManager()
                .createQuery("SELECT NEW Playlist_Management.PlayListSongMeta(sng, art, mcg) FROM Song sng JOIN sng.albumId alb LEFT JOIN alb.musicgroup mcg LEFT JOIN alb.artist art WHERE art.artisticname LIKE '%" + key_str + "%' OR mcg.name LIKE '%" + key_str + "%' OR sng.title LIKE '%" + key_str + "%'", PlayListSongMeta.class)
                .getResultList();
        list1.clear();
        list1.addAll(PlayList_SongMeta);
    }//GEN-LAST:event_SearchBtnActionPerformed

    /**
     * @param args the command line arguments
     */
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelBtn;
    private javax.swing.JLabel ErrorMessage;
    private javax.persistence.EntityManager Radiostation_ProjectPUEntityManager;
    private javax.swing.JButton SaveBtn;
    private javax.swing.JButton SaveBtn1;
    private javax.swing.JTable SongListTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JLabel lbl1;
    private java.util.List<Playlist_Management.PlayListSongMeta> list1;
    private javax.persistence.Query query1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
